package login

import (
	"AI/sql/disposition"
	status "AI/util"
	"database/sql"
	"fmt"
	"net/http"
	"regexp"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func Registered(Data map[string]string, c *gin.Context) {
	Account := Data["Account"]
	// if len([]rune(Account)) < 5 || len([]rune(Account)) > 12 {
	// 	c.JSON(http.StatusOK, gin.H{
	// 		"code": status.Dataerrors,
	// 		"msg":  "手机号错误,请输入正确的手机号",
	// 	})
	// 	return
	// }
	pws := Data["pws"]
	if len([]rune(pws)) < 5 || len([]rune(pws)) > 13 {
		c.JSON(http.StatusOK, gin.H{
			"code": status.Dataerrors,
			"msg":  "密码需要大于5位小于32位字母或数组",
		})
		return
	}
	phoneNumber := Data["Account"]
	ph, _ := regexp.Compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$")
	fmt.Println(phoneNumber)
	matched := ph.MatchString(Account)
	fmt.Println(matched, "/n")
	if !matched {
		c.JSON(http.StatusOK, gin.H{
			"code": status.Dataerrors,
			"msg":  "手机号错误,请输入正确的手机号",
		})
		return
	}
	code := Data["code"]
	if len([]rune(code)) != 4 {
		c.JSON(http.StatusOK, gin.H{
			"code": status.Dataerrors,
			"msg":  "验证码错误",
		})
		return
	}
	// 开始操作数据库
	newUser(Data, c)

}
func newUser(Data map[string]string, c *gin.Context) {
	// code, err := Data["code"]
	phoneNumber := Data["Account"]
	pws := Data["pws"]
	// 先把账号和手机号关联起来 暂时不需要 账号 Data["Account"]
	Account := Data["Account"]

	// 连接数据库
	db, err := sql.Open(disposition.Usersql["sqlname"], disposition.Usersql["sqlurl"])
	// 获取当前时间
	currentTime := GetTime()
	fmt.Println(currentTime)
	getUserinfo := map[string]interface{}{"Account": Account, "creatTime": currentTime}
	token := SetToken(db, c, getUserinfo)
	sqls := fmt.Sprintf(`insert into userinfo (phoneNumber,paswrod,Account,status,creatTime,username,token) values ("%s","%s","%s","%s","%s","%s","%s")`, phoneNumber, pws, Account, "1", currentTime, "新用户", token)

	// 先查询是否有该用户
	queryUser := fmt.Sprintf(`select Account,phoneNumber from userinfo where Account = "%s" or  phoneNumber = "%s" `, Account, phoneNumber)
	stm, err := db.Prepare(queryUser)
	queyUser, err := stm.Query()
	if err != nil || !queyUser.Next() {
		rows, err := db.Exec(sqls)
		fmt.Println(rows, err)
		// 查询 用户信息
		QuerInfoSql := fmt.Sprintf(`select  id,username,Account,status,token,phoneNumber,creatTime from userinfo   where token = "%s"`, token)
		Usersql, _ := db.Query(QuerInfoSql)
		var userinfo info
		for Usersql.Next() {
			Usersql.Scan(&userinfo.id, &userinfo.username, &userinfo.Account, &userinfo.status, &userinfo.token, &userinfo.phoneNumber, &userinfo.creatTime)
		}
		resolveinfo := map[string]interface{}{"username": userinfo.username, "Account": userinfo.Account, "phoneNumber": userinfo.phoneNumber, "creatTime": userinfo.creatTime, "status": userinfo.status, "token": userinfo.token}
		c.JSON(http.StatusOK, gin.H{
			"code": status.Success,
			"msg":  "注册成功",
			"data": resolveinfo,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": status.Dataerrors,
			"msg":  "该账号已注册",
		})
		return
	}

	defer db.Close()
	// fmt.Println(rows)
	fmt.Println(sqls, db, err)
	return

}
func GetTime() string {
	t1 := time.Now().Year() //年

	t2 := time.Now().Month() //月

	t3 := time.Now().Day() //日

	t4 := time.Now().Hour() //小时

	t5 := time.Now().Minute() //分钟

	t6 := time.Now().Second() //秒

	currentTimeData := fmt.Sprintf(`%s-%s-%s %s:%s:%s `, strconv.Itoa(t1), t2, strconv.Itoa(t3), strconv.Itoa(t4), strconv.Itoa(t5), strconv.Itoa(t6))
	fmt.Println(currentTimeData)
	return time.Now().Format("2006-01-02 15:04:05")
}
